// MainDemoLauncher.cpp : implementation file
//

#include "stdafx.h"
#include "LightspeedTraderAPIDemo.h"
#include "MainDemoLauncherDlg.h"

#include "AcctDlg.h"
#include "SummaryDlg.h"
#include "PositionDlg.h"
#include "OrderDlg.h"
#include "OrderEntryDlg.h"
#include "FullQuoteDlg.h"
#include "CancelCloseDlg.h"
#include "ChartPointsDlg.h"
#include "ShortPosLimitDlg.h"
#include "PairCalculatorMain.h"

// MainDemoLauncher dialog

IMPLEMENT_DYNAMIC(MainDemoLauncherDlg, CModelessDialog)

MainDemoLauncherDlg::MainDemoLauncherDlg(CWnd* pParent /*=NULL*/)
	: CModelessDialog(MainDemoLauncherDlg::IDD, pParent)
	, m_StatusEdit(CEdit())
{
}

MainDemoLauncherDlg::~MainDemoLauncherDlg()
{
	L_DetachFromAppNotifier(this);
	L_UnsubscribeFromMarketStatus(this);
}

BOOL MainDemoLauncherDlg::Create(CWnd *pParentWnd /*= CWnd::FromHandle(L_GetMainWnd())*/)
{
	return __super::Create(MainDemoLauncherDlg::IDD, pParentWnd);
}

void MainDemoLauncherDlg::DoDataExchange(CDataExchange* pDX)
{
	__super::DoDataExchange(pDX);

	DDX_Control(pDX, IDC_STATUSEDIT, m_StatusEdit);
}

BOOL MainDemoLauncherDlg::OnInitDialog()
{
	__super::OnInitDialog();

	L_AttachToAppNotifier(this);
	L_SubscribeToMarketStatus(this);
	UpdateStatus();

	return TRUE;
}

void MainDemoLauncherDlg::UpdateStatus()
{
	CString marketStatus = "";

	switch (L_MarketStatus())
	{
	case L_Market::MorningBefore: marketStatus = "Morning Before"; break;
	case L_Market::Premarket: marketStatus = "Premarket"; break;
	case L_Market::Preopening: marketStatus = "Proeopening"; break;
	case L_Market::RegSessionOpen: marketStatus = "Reg Session"; break;
	case L_Market::ExtSessionOpen: marketStatus = "Ext Session"; break;
	case L_Market::Closed: marketStatus = "Closed"; break;
	case L_Market::Halted: marketStatus = "Halted"; break;
	case L_Market::Resumed: marketStatus = "Resumed"; break;
	}

	CString executorStatus = (L_IsExecutorConnected() ? "Connected" : "Disconnected");
	CString quoteStatus = (L_IsQuoteConnected() ? "Connected" : "Disconnected");

	CString status;
	status.Format("Market Status: %s   Executor Status: %s   Quote Status: %s"
		, marketStatus
		, executorStatus
		, quoteStatus);

	m_StatusEdit.SetWindowText(status);
}

void MainDemoLauncherDlg::HandleMessage(L_Message const *pMsg)
{
	switch(pMsg->L_Type())
	{
	case L_MsgLinkStatus::id:
	case L_MsgMarketStatus::id:
		UpdateStatus();
		break;
	}
}

BEGIN_MESSAGE_MAP(MainDemoLauncherDlg, CModelessDialog)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_ACCOUNTDEMO, OnBnClickedAccountDemo)
	ON_BN_CLICKED(IDC_SUMMARYDEMO, OnBnClickedSummaryDemo)
	ON_BN_CLICKED(IDC_POSITIONDEMO, OnBnClickedPositionDemo)
	ON_BN_CLICKED(IDC_ORDERDEMO, OnBnClickedOrderDemo)
	ON_BN_CLICKED(IDC_ORDERENTRY_VIEW, OnBnClickedOrderEntryView)
	ON_BN_CLICKED(IDC_FULLQUOTE_VIEW, OnBnClickedFullQuoteView)
	ON_BN_CLICKED(IDC_CANCELCLOSEDEMO, OnBnClickedCancelCloseDemo)
	ON_BN_CLICKED(IDC_CHARTPOINTS_VIEW, OnBnClickedChartPointsView)
	ON_BN_CLICKED(IDC_SHORTLIMIT_VIEW, OnBnClickedShortLimitView)
	ON_BN_CLICKED(IDC_VIEWPAIRMAIN, OnBnClickedPairCalcMainView)
END_MESSAGE_MAP()

// MainDemoLauncher message handlers

void MainDemoLauncherDlg::OnClose()
{
	__super::OnClose();
	L_ExitLightspeedExtension(0);
}

void MainDemoLauncherDlg::OnBnClickedAccountDemo()
{
	AcctDlg *pDlg = new AcctDlg;

	if (! CLightspeedTraderAPIDemoApp::GetInstance()->CreateDlg(pDlg))
	{
		delete pDlg;
	}
}

void MainDemoLauncherDlg::OnBnClickedSummaryDemo()
{
	SummaryDlg *pDlg = new SummaryDlg;
	
	if (! CLightspeedTraderAPIDemoApp::GetInstance()->CreateDlg(pDlg))
	{
		delete pDlg;
	}
}

void MainDemoLauncherDlg::OnBnClickedPositionDemo()
{
	PositionDlg *pDlg = new PositionDlg;
	if (! CLightspeedTraderAPIDemoApp::GetInstance()->CreateDlg(pDlg))
	{
		delete pDlg;
	}
}

void MainDemoLauncherDlg::OnBnClickedOrderDemo()
{
	OrderDlg *pDlg = new OrderDlg;
	if (! CLightspeedTraderAPIDemoApp::GetInstance()->CreateDlg(pDlg))
	{
		delete pDlg;
	}
}

void MainDemoLauncherDlg::OnBnClickedOrderEntryView()
{
	OrderEntryDlg *pDlg = new OrderEntryDlg;
	if (! CLightspeedTraderAPIDemoApp::GetInstance()->CreateDlg(pDlg))
	{
		delete pDlg;
	}
}

void MainDemoLauncherDlg::OnBnClickedFullQuoteView()
{
	FullQuoteDlg *pDlg = new FullQuoteDlg;
	if (! CLightspeedTraderAPIDemoApp::GetInstance()->CreateDlg(pDlg))
	{
		delete pDlg;
	}
}

void MainDemoLauncherDlg::OnBnClickedCancelCloseDemo()
{
	CancelCloseDlg *pDlg = new CancelCloseDlg;
	if (! CLightspeedTraderAPIDemoApp::GetInstance()->CreateDlg(pDlg))
	{
		delete pDlg;
	}
}

void MainDemoLauncherDlg::OnBnClickedChartPointsView()
{
	ChartPointsDlg *pDlg = new ChartPointsDlg;
	if (! CLightspeedTraderAPIDemoApp::GetInstance()->CreateDlg(pDlg))
	{
		delete pDlg;
	}
}

void MainDemoLauncherDlg::OnBnClickedShortLimitView()
{
	ShortPosLimitDlg *pDlg = new ShortPosLimitDlg;
	if (! CLightspeedTraderAPIDemoApp::GetInstance()->CreateDlg(pDlg))
	{
		delete pDlg;
	}
}

void MainDemoLauncherDlg::OnBnClickedPairCalcMainView()
{
	PairCalculatorMain *pDlg = new PairCalculatorMain;
	if (! CLightspeedTraderAPIDemoApp::GetInstance()->CreateDlg(pDlg))
	{
		delete pDlg;
	}
}